package com.net.xpay.clean.scheduler;


import com.google.common.base.Preconditions;
import com.net.common.util.LocalDateUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.stat.CompanyDailyStat;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.stat.CompanyDailyStatManager;
import com.net.xpay.common.manager.sync.CommonPayTransBatchManager;
import com.net.xpay.common.manager.sync.CommonSafePayBatchManager;
import com.net.xpay.core.manager.ChargeRecordManager;
import com.net.xpay.core.manager.PayTransBatchManager;
import com.net.xpay.core.manager.PayTransManager;
import com.net.xpay.core.manager.safePay.SafePayBatchManager;
import com.net.xpay.core.manager.safePay.SafePayOrderManager;
import com.net.xpay.core.model.SafePayTransBankDailyStatModel;
import com.net.shard.ShardDataSource;
import com.net.shard.ShardDataSourceConfigurer;
import com.net.shard.ShardUtil;
import com.net.shard.domain.ShardConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.sql.DataSource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 2019/7/15 16:28

 * 修复或者初始化数据
 */
@Slf4j
//@Component
public class CompanyDailyStatAddColumnScheduler {

    @Autowired
    private UserManager userManager;

    @Autowired
    private PayTransBatchManager payTransBatchManager;

    @Autowired
    private SafePayBatchManager safePayBatchManager;

    @Autowired
    private CommonPayTransBatchManager commonPayTransBatchManager;

    @Autowired
    private CommonSafePayBatchManager commonSafePayBatchManager;

    @Autowired
    private ChargeRecordManager chargeRecordManager;

    @Autowired
    private PayTransManager payTransManager;

    @Autowired
    private SafePayOrderManager safePayOrderManager;

    @Autowired
    private CompanyDailyStatManager companyDailyStatManager;

    @Autowired
    @Qualifier("shardDataSource")
    private DataSource shardDataSource;


//    @Scheduled(cron = "0 50 20 * * ?")
    public void updateCompanyDailyStat() {
        //查出所有的 for循环处理单条更新
        List<CompanyDailyStat> list = companyDailyStatManager.listAll();
        for (CompanyDailyStat stat : list) {
            stat.setTotalTransBankAmount(new BigDecimal("0.00"));
            stat.setTotalTransAlipayAmount(new BigDecimal("0.00"));
            stat.setTotalSuccessTransBankAmount(new BigDecimal("0.00"));
            stat.setTotalSuccessTransAlipayAmount(new BigDecimal("0.00"));

            stat.setSafePayTransBankAmount(new BigDecimal("0.00"));
            stat.setSafePayTransAlipayAmount(new BigDecimal("0.00"));
            stat.setSafePayTransSuccessBankAmount(new BigDecimal("0.00"));
            stat.setSafePayTransSuccessAlipayAmount(new BigDecimal("0.00"));
            statCompanyDailyStat(stat);
        }
    }

    private void statCompanyDailyStat(CompanyDailyStat stat) {
        ShardDataSourceConfigurer shardDataSourceConfigurer = ((ShardDataSource) shardDataSource).getShardDataSourceConfigurer();
        Map<Long, ShardConfig> shardConfigMap = shardDataSourceConfigurer.getShardConfigMap();
        Preconditions.checkArgument(CollectionUtils.isNotEmpty(shardConfigMap.keySet()));
        LocalDate day = stat.getDay();
        // 获取当天的起始时间和终止时间 终止时间保留在23:59:59.999999999
        LocalDateTime minEndTime = LocalDateUtil.getDayStart(day);
        LocalDateTime maxEndTime = LocalDateUtil.getDayEnd(day);
        //分库
        for (Long shardId : shardConfigMap.keySet()) {
            try {
                ShardUtil.setShardContext(shardId);
                log.info("statDailyCompanyBill start in shard={}|day={}", shardId, day);

                //统计数据
                saveCompanyDailyStat(minEndTime, maxEndTime, stat);

                log.info("statDailyCompanyBill end in shard={}|day={}", shardId, day);
            } finally {
                ShardUtil.cleanShardContext();
            }
        }
        companyDailyStatManager.update(stat);
    }

    private void saveCompanyDailyStat(LocalDateTime minEndTime, LocalDateTime maxEndTime, CompanyDailyStat stat) {
        //安全发到卡转账金额
        SafePayTransBankDailyStatModel safePayTransBankDailyStatModel = safePayBatchManager.statSafePayTransBankAmount(minEndTime, maxEndTime);
        log.info("safePayTransBankDailyStatModel={}", SerializationUtil.obj2String(safePayTransBankDailyStatModel));
        //设值
        //安全发到卡总金额 & 成功金额
        stat.setSafePayTransBankAmount(stat.getSafePayTransBankAmount().add(safePayTransBankDailyStatModel.getSafePayTransBankAmount()));
        stat.setSafePayTransSuccessBankAmount(stat.getSafePayTransSuccessBankAmount().add(safePayTransBankDailyStatModel.getSafePayTransSuccessBankAmount()));
        //安全发到支付宝总金额 & 成功金额
        stat.setSafePayTransAlipayAmount(stat.getSafePayTransAmount().subtract(stat.getSafePayTransBankAmount()));
        stat.setSafePayTransSuccessAlipayAmount(stat.getSafePayTransSuccessAmount().subtract(stat.getSafePayTransSuccessBankAmount()));
        //到卡总金额 & 总成功金额 只有安全发
        stat.setTotalTransBankAmount(stat.getTotalTransBankAmount().add(stat.getSafePayTransBankAmount()));
        stat.setTotalSuccessTransBankAmount(stat.getTotalSuccessTransBankAmount().add(stat.getSafePayTransSuccessBankAmount()));
        //到支付宝总金额 & 总成功金额
        stat.setTotalTransAlipayAmount(stat.getSafePayTransAlipayAmount().add(stat.getPayTransAmount()));
        stat.setTotalSuccessTransAlipayAmount(stat.getPayTransSuccessAmount().add(stat.getSafePayTransAlipayAmount()));
    }

}
